"""
WAF Tool to select the correct toolchain based on the target archtecture.

This tool loads compiler_c and compiler_cxx, so you don't need to load them
(and you must not load them before this tool). Use the environment variable
TOOLCHAIN to define the toolchain.

Example::

    def configure(cfg):
        cfg.env.TOOLCHAIN = 'arm-linux-gnueabihf'
        cfg.load('toolchain')
"""

from waflib.Configure import conf

@conf
def find_gcc(conf):
    names = ['gcc', 'cc']
    if conf.env.TOOLCHAIN != 'native':
        names = ['%s%s' % (conf.env.TOOLCHAIN, n) for n in names]
    cc = conf.find_program(names, var='CC')
    conf.get_cc_version(cc, gcc=True)
    conf.env.CC_NAME = 'gcc'
    
@conf
def find_gxx(conf):
    names = ['g++', 'c++']
    if conf.env.TOOLCHAIN != 'native':
        names = ['%s%s' % (conf.env.TOOLCHAIN, n) for n in names]
    cxx = conf.find_program(names, var='CXX')
    conf.get_cc_version(cxx, gcc=True)
    conf.env.CXX_NAME = 'gcc'
    
@conf
def find_ar(conf):
    """Configuration helper used by C/C++ tools to enable the support for static libraries"""
    names = ['ar','ar']
    if conf.env.TOOLCHAIN != 'native':
        names = ['%s%s' %(conf.env.TOOLCHAIN, n) for n in names]
    ar = conf.find_program(names, var='AR')
    conf.env.AR_NAME = 'ar'
    

def configure(cfg):
    cfg.find_program('%sgcc' % cfg.env.TOOLCHAIN, var='AS', quiet=True)
    cfg.load('gas compiler_c compiler_cxx ar')
    

